package cn.edu.zafu.bigdata.x.xiejunjie.networkprogramming;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class TimeClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        InetAddress serverAddress = InetAddress.getByName("localhost"); // 可以改为同学电脑的IP地址

        String request = "time";
        byte[] sendData = request.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 8888);

        socket.send(sendPacket); // 发送时间请求到服务端

        byte[] receiveBuffer = new byte[1024];
        DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
        socket.receive(receivePacket); // 接收服务端的响应
        String timeResponse = new String(receivePacket.getData(), 0, receivePacket.getLength());

        System.out.println("服务端时间: " + timeResponse);

        socket.close(); // 关闭socket
    }
}
